<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include("base.php");

  class ConferenceController extends BaseController
  {  
    var $_name = 'conference';
    
    function add(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      
      $view->setModel($this->getModel());
      $view->setModel($this->getModel('team','teammodel'));
      parent::add();
    }
    
    function edit(){
      
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
      
      $view->setModel($this->getModel());
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel('conferenceteam', 'conferenceteammodel'));
      parent::edit();
    }
    
    function save()
    {
      $data       = JRequest::get('POST');
      $data['description'] = JRequest::getVar( 'description', '', 'post', 'string', JREQUEST_ALLOWHTML );
       
      $model      = & $this->getModel($this->_name); 
      $conference = $model->save($data);
      
      $conference_team = $this->getModel('conferenceteam', 'conferenceteammodel');
      $conference_team->renew_teams($conference->id, $data['teams']);
       
      $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option').'&c='.JRequest::getVar('c'), false);
      $this->setRedirect($redirectTo,  ucfirst($this->_name) . ' Saved!');             
    }
    
    function remove()
    {
      $ids = JRequest::getVar('cid', null, 'default', 'array');
      if($ids === null)
        JError::raiseError(500, 'cid parameter missing from the request');
       
      $conference_team = & $this->getModel('conferenceteam', 'conferenceteammodel');
      foreach($ids as $id){
        $conference_teams = $conference_team->get_by_conference_id($id);
        $ct_ids           = array();
        foreach($conference_teams as $ct)
          array_push($ct_ids, $ct->id);
        if(!empty($ct_ids))
          $conference_team->delete($ct_ids);
      }
      parent::remove();
    }
  }
?>